//Copyright 2010 Balazs Keri, Imre Keri
//
//This file is part of FireworksCreator.
//
//FireworksCreator is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//FireworksCreator is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with FireworksCreator.  If not, see <http://www.gnu.org/licenses/>.
//
#include <model/particletemplate.h>
#include "viewattributes.h"
#include "primitives.h"
using namespace ViewAttributeType;
//
ModelDataObject::ModelDataObject(const QSharedPointer<ViewAttributeGroup> &p_container,ViewAttributeName p_name)
:
   ViewAttribute(p_container,p_name),
   object(0)
{
}
void ModelDataObject::handle_post_primitive_pre_view(const Primitive &prim)
{
   if(prim.type==Primitives::type_DelDataObject)
   {
      const Primitives::DelDataObject &p=static_cast<const Primitives::DelDataObject&>(prim);
      if(p.get_old_object_id()==object_id)
      {
         set_object(0,0);
      }
   }
   else if(prim.type==Primitives::type_Reset)
   {
      set_object(0,0);
   }
}
//
PTTimeMark::PTTimeMark(const QSharedPointer<ViewAttributeGroup> &p_container,ViewAttributeName p_name)
:
   ViewAttribute(p_container,p_name),
   particle_template(0)
{
}
void PTTimeMark::set_particle_template(model::ParticleTemplate *p_particle_template,QString p_tm_name,View *origin)
{
   particle_template=p_particle_template;
   if(particle_template)
   {
      id=particle_template->get_id();
      if(p_tm_name.isEmpty()||particle_template->time_marks.contains(p_tm_name))
         tm_name=p_tm_name;
      else
      {
         tm_name="";
         qDebug()<<"PTTimeMark: particle template \""<<particle_template->get_name()<<"\" ("<<particle_template<<") unknown time mark \""<<p_tm_name<<"\"";
      }
   }
   else
   {
      id=Id();
      tm_name="";
      if(!p_tm_name.isEmpty())
         qDebug()<<"PTTimeMark: set time mark name \""<<p_tm_name<<"\" at empty particle template";
   }
   emit_value_changed(origin);
}
void PTTimeMark::set_tm_name(const QString &p_tm_name,View *origin)
{
   if(!particle_template)
   {
      if(!p_tm_name.isEmpty())
         qDebug()<<"PTTimeMark: set time mark name \""<<p_tm_name<<"\" at empty particle template";
   }
   else
   {
      if(p_tm_name.isEmpty()||particle_template->time_marks.contains(p_tm_name))
         tm_name=p_tm_name;
      else
      {
         tm_name="";
         qDebug()<<"PTTimeMark: particle template \""<<particle_template->get_name()<<"\" ("<<particle_template<<") unknown time mark \""<<p_tm_name<<"\"";
      }
      emit_value_changed(origin);
   }
}
void PTTimeMark::handle_post_primitive_pre_view(const Primitive &prim)
{
   using namespace Primitives;
   if(prim.type==type_DelDataObject)
   {
      const DelDataObject &p=static_cast<const DelDataObject&>(prim);
      if(p.get_old_object_id()==id)
      {
         set_particle_template(0,"",0);
      }
   }
   else if(prim.type==type_Reset)
   {
      set_particle_template(0,"",0);
   }
   else if(prim.type==type_SelectPT)
   {
      const SelectPT &p=static_cast<const SelectPT&>(prim);
      if(p.get_pt_id()==id)
      {
         switch(p.get_delegate().get_type())
         {
         case type_PTRemTimeMark:
         case type_PTNewTimeMark:
            // watch out for undo/redo cases
            if(particle_template->time_marks.count(tm_name)==0)
            {
               set_tm_name("",0);
            }
            break;
         case type_PTRenameTimeMark:
            {
               const PTRenameTimeMark &d=static_cast<const PTRenameTimeMark&>(p.get_delegate());
               if(d.get_other_name()==tm_name)
               {
                  set_tm_name(d.get_current_name(),0);
               }
               break;
            }
         }
      }
   }
}
